package com.zhkj.txg.module.home.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.txg.module.home.entity.MallCategoryResponse
import com.zhkj.txg.module.home.model.HomeManager

class MallCategoryViewModel(application: Application) : BaseViewModel(application) {
    private val homeManager = HomeManager()
    val shopListLiveData = MutableLiveData<HttpResponse<MallCategoryResponse>>()

    var cateId: Int = 0

    private var page = 1
    private val numb = 20

    fun mallCategory(
        token: String,
        type: Int,
        restart: Boolean = false
    ) {
        val listener = object : ViewModelSubscribeListener<MallCategoryResponse>(shopListLiveData) {
            override fun onSuccess(resp: MallCategoryResponse) {
                resp.data?.hasMore =
                    if (type == 0) resp.data?.shops?.size ?: 0 >= numb else resp.data?.goods?.size ?: 0 >= numb
                resp.data?.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (restart) page = 1
        val list = homeManager.mallCategoryList(token, cateId, type, page, numb, listener)
        dispose.add(list)
    }
}